package dao;

import classes.Tarefa;
import classes.TarefaAndamento;
import conexao.GerenciadorConexao;
import erro.ConexaoException;
import erro.ConsultaException;
import gui.TarefaMostrar;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author Roberta
 */
public class TarefaAndamentoDao {

    private GerenciadorConexao g;

    public TarefaAndamentoDao() {
        g = GerenciadorConexao.getInstancia();
    }

    /**
     * 
     * @param ta (TarefaAndamento)
     * @throws ConexaoException
     * @throws ConsultaException 
     */
    public void inserir(TarefaAndamento ta) throws ConexaoException, ConsultaException {
        String sql = "INSERT INTO tarefaandamento (tarefa_id, data, obs, anexo) VALUES (?,?,?,?)";
        try {
            PreparedStatement pstm = (PreparedStatement) g.connect().prepareStatement(sql);
            Date data = ta.getData();
            ta.setData(data);
            pstm.setInt(1, ta.getTarefa_id().getTarefa_id());
            pstm.setDate(2, new java.sql.Date(data.getTime()));
            pstm.setString(3, ta.getObs());
            pstm.setString(4, ta.getAnexo());
            pstm.executeUpdate();

        } catch (SQLException ex) {
            throw new ConsultaException(ex);
        } finally {
            g.disconnect();
        }
    }

    /**
     * 
     * @param ta (TarefaAndamento)
     * @throws ConexaoException
     * @throws ConsultaException 
     */
    public void alterar(TarefaAndamento ta) throws ConexaoException, ConsultaException {
        String sql = "UPDATE tarefaandamento SET data=?,obs=? WHERE idtarefaAndamento=?";
        try {
            PreparedStatement pstm = (PreparedStatement) g.connect().prepareStatement(sql);
            pstm.setDate(1, new java.sql.Date(ta.getData().getTime()));
            pstm.setString(2, ta.getObs());

            pstm.setInt(3, ta.getIdAndamento());
            pstm.executeUpdate();
        } catch (SQLException ex) {
            throw new ConsultaException(ex);
        } finally {
            g.disconnect();
        }
    }

    /**
     * 
     * @param ta (TarefaAndamento)
     * @return
     * @throws ConexaoException
     * @throws ConsultaException 
     */
    public TarefaAndamento pesquisar(TarefaAndamento ta) throws ConexaoException, ConsultaException {

        String sql = "SELECT * FROM tarefaandamento WHERE idtarefaAndamento=?";
        try {
            PreparedStatement pstm = g.connect().prepareStatement(sql);
            pstm.setInt(1, ta.getIdAndamento());
            ResultSet rs = pstm.executeQuery();
            if (rs.next()) {
                ta.setIdAndamento(rs.getInt("idtarefaAndamento"));
                ta.setObs(rs.getString("obs"));
                ta.setData(rs.getDate("data"));
            }
        } catch (SQLException ex) {
            throw new ConsultaException(ex);
        } finally {
            g.disconnect();
        }
        return ta;
    }

    /**
     * 
     * @return
     * @throws ConexaoException
     * @throws ConsultaException 
     */
    public ArrayList<TarefaAndamento> listar() throws ConexaoException, ConsultaException {
        ArrayList<TarefaAndamento> lista = new ArrayList<TarefaAndamento>();
        TarefaAndamento ta = new TarefaAndamento();
        int id = TarefaMostrar.mat;
        Tarefa t = new Tarefa();
        t.setTarefa_id(id);
        ta.setTarefa_id(t);
        String sql = "SELECT * FROM tarefaandamento WHERE tarefa_id=?";
        try {
            PreparedStatement pstm = g.connect().prepareStatement(sql);
            pstm.setInt(1, ta.getTarefa_id().getTarefa_id());
            ResultSet rs = pstm.executeQuery();
            while (rs.next()) {
                ta = new TarefaAndamento();
                ta.setObs(rs.getString("obs"));
                ta.setData(rs.getDate("data"));
                ta.setIdAndamento(rs.getInt("idtarefaAndamento"));
                lista.add(ta);
            }
        } catch (SQLException ex) {
            throw new ConsultaException(ex);
        } finally {
            g.disconnect();
        }
        return lista;
    }

    /**
     * 
     * @param ta (TarefaAndamento)
     * @throws ConexaoException
     * @throws ConsultaException 
     */
    public void deletarAnd(TarefaAndamento ta) throws ConexaoException, ConsultaException {
        String sql = "DELETE FROM tarefaandamento WHERE tarefa_id=?";
        try {
            PreparedStatement pstm = (PreparedStatement) g.connect().prepareStatement(sql);
            pstm.setInt(1, TarefaMostrar.mat);
            pstm.executeUpdate();
        } catch (SQLException ex) {
            throw new ConsultaException(ex);
        } finally {
            g.disconnect();
        }
    }

    /**
     * 
     * @param ta(TarefaAndamento)
     * @throws ConexaoException
     * @throws ConsultaException 
     */
    public void deletarAndamento(TarefaAndamento ta) throws ConexaoException, ConsultaException {
        String sql = "DELETE FROM tarefaandamento WHERE idtarefaAndamento=?";
        try {
            PreparedStatement pstm = (PreparedStatement) g.connect().prepareStatement(sql);
            pstm.setInt(1, TarefaMostrar.codigo);
            pstm.executeUpdate();
        } catch (SQLException ex) {
            throw new ConsultaException(ex);
        } finally {
            g.disconnect();
        }
    }
}
